<?php

class IndexController extends AppBaseC {

    //首页展示
    public function actionIndex() {

        $imgs = array();
        $goods = array();
        $imgs = AdvertisingModel::getListType(1);

        //$this->layout = 'theme.views.layouts.frontNoContent';
        $pagesize = Yii::app()->request->getParam('pagesize');
        $page = Yii::app()->request->getParam('page');
        $cid = Yii::app()->request->getParam('cid');

        $mid = Yii::app()->request->getParam('mid');
        $page = empty($page) ? 1: $page;//第几页 
        $pagesize = empty($pagesize) ? 10: $pagesize;//第几页         
        $cid = empty($cid) ? '': $cid;//分类Id        
        $mid = empty($mid) ? '': $mid;//分类Id 
        $order = Yii::app()->request->getParam('order');
        $extra = Yii::app()->request->getParam('extra');
        $extra = empty($extra) ? '': $extra;//额外
        $order = empty($order) ? 9: $order;//排序

        if($order == 9){
    //1模块ID 2分类ID  3专场ID  4标签列表5是否首页6是否免邮 7额外条件    8排序 9第几页 10每页展示条数11是否打乱顺序 12是否缓存 13缓存时间
            $goods = GoodsModel::getVariedGoods($mid,$cid,'','','','', $extra, $order,$page,$pagesize,true,true,600);            
        }else{
    //1模块ID 2分类ID  3专场ID  4标签列表5是否首页6是否免邮 7额外条件    8排序 9第几页 10每页展示条数11是否打乱顺序 12是否缓存 13缓存时间
            $goods = GoodsModel::getVariedGoods($mid,$cid,'','','','', $extra, $order,$page,$pagesize,false,true,600);             
        }

        if(empty($goods['page'])){
            unset($goods['page']);
        }     


        $return = array(
            'imgs' => $imgs,
            'goods' => $goods,
        );
        echo CJSON::encode($return);
    }


    public function actionGetcategory(){
        $arrSignMenu = CategoryModel::getListMenu(2); 
        echo CJSON::encode($arrSignMenu);       
    }

    //下棋预告
    public function actionNs() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppNextSales($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //秋冬专题
    public function actionQd() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppWinterNewGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //天猫专场
    public function actionTm() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppTianMaoGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //淘宝
    public function actionTb() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getTBGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //京东页面展示
    public function actionJd() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppJdGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //九块九 页面展示
    public function actionJkj() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppJKJGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );

        echo CJSON::encode($return);
    }

    //今日特惠 页面展示
    public function actionHome() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getHomeGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }

    //今日特惠按价格排序输出接口
    public function actionTodayPrice() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getHomeGoods($num, true, 'promo_price');
        $data = array();
        foreach ($goods['data'] as $rs) {
            $data[] = $rs;
        }
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $data,
        );
        echo CJSON::encode($return);
    }

    //今日特惠按价格排序输出接口
    public function actionTodaySales() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $order = "sales";
        $goods = $GoodModel->getHomeGoods($num, $cache = true, $order);
        $data = array();
        foreach ($goods['data'] as $op){
            $data[] = $op;
        }
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $data,
        );
        echo CJSON::encode($return);
    }

    //分类栏目下的商品列表
    public function actionCat() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getAppCatGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' => $goods['data'],
        );
        echo CJSON::encode($return);
    }
    //获取栏目列表
    public function actionCatlist(){
        $model = new AppGoodsModel();
        echo CJSON::encode($model->getCatlist());
    }

    /**
     * 获取积分兑换商品列表
     */
     public function actionExch() {
        $GoodModel = new AppGoodsModel();
        $num = (int) Yii::app()->request->getParam('num');
        if ($num <= 0) {
            $num = 20;
        }
        $goods = $GoodModel->getExchangeGoods($num);
        $return = array(
            'status' => 1,
            'total' => $goods['total'],
            'count' => count($goods['data']),
            'detail' =>$goods['data'],
        );       
        echo CJSON::encode($return);
        
    }
    /**
     * 用户下载
     * @return [type] [description]
     */
    public function actionDownload()
    {
        if(stripos($_SERVER['HTTP_USER_AGENT'],'iphone') === false){//安卓
            $this->redirect('http://a.app.qq.com/o/simple.jsp?pkgname=com.yzw.activity');
        }else{//iphone
//            header('Content-Type: text/html; charset=UTF-8');
//            exit('ios版本开发中,敬请期望');
            $this->redirect('https://itunes.apple.com/us/app/1zhe-wang/id914988082?l=zh&ls=1&mt=8');
        }       
    }


}
